package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.Agent;
import beans.Branch;

import utils.HibernateUtil;




public class BranchDao {
	private SessionFactory sessionFactory;

	public BranchDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	AgentDao agentDao = new AgentDao();
	
	/**
     * 
     * 获取网点分页
     * @author Devil Zheng
     * @created 2015-9-5 下午1:56:11
     * @param agent
     * @return
     */
    public List<Branch> getBranches(Agent agent,int rows,int page) {
        Session session = sessionFactory.openSession();
        tr = session.beginTransaction();
        Criteria c = session.createCriteria(Branch.class);
        c.add(Restrictions.eq("agent", agent));
        c.setFirstResult((page-1)*rows);
        c.setMaxResults(rows);
        c.addOrder(Order.asc("branchId"));
        List<Branch> branchs = c.list();
        tr.commit();
        session.close();
        return branchs;
    }
	
	/**
	 * 返回所有网点信息
	 */
	public List<Branch> getAllbranch(){
		Session session=sessionFactory.openSession();
		
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Branch.class);
		List<Branch> branchs=c.list();
		tr.commit();
		
		session.close();
		return branchs;
	}
	
	/**
	 * 根据代理商id返回网点集合
	 */
	public List<Branch> getBranchByAgentId(int agentId){
		AgentDao agentDao = new AgentDao();
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Branch.class);
		c.add(Restrictions.in("agent", agentDao.getChildAndParentAgent(agentId)));
		List<Branch> branchs=c.list();
		tr.commit();
		session.close();
		return branchs;
	}

	/**
	 * 通过网点ID返回指定网点信息
	 * 
	 * @param branchId
	 *            网点ID
	 * @return
	 */
	public Branch getBranchById(int branchId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Branch branch = (Branch) session.get(Branch.class, branchId);
		tr.commit();
		session.close();
		return branch;
	}
	
	/**
	 * 删除网点
	 */
	public void delete(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(branch);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改网点
	 */
	public void edit(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(branch);
		tr.commit();
		session.close();
	}

	/**
	 * 获取所有网点的列表
	 */
	public List<Branch> getAll(Agent agent) {
		List<Agent> agents = agentDao.getChildAgent(agent);
		agents.add(agent);
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.in("agent", agents));
		c.addOrder(Order.asc("branchId"));
		List<Branch> branchs = c.list();
		tr.commit();
		session.close();
		return branchs;
	}

	/**
	 * 保存新增的网点
	 */
	public void save(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(branch);
		tr.commit();
		session.close();
	}
}
